package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.JingfeiDanweiqueren;
import com.ruoyi.system.tools.BooleanMessage;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.JingfeiDanweijingfei;
import com.ruoyi.system.service.IJingfeiDanweijingfeiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * deptjingfeiController
 * 
 * @author ruoyi
 * @date 2024-07-11
 */
@Controller
@RequestMapping("/system/danweijingfei")
public class JingfeiDanweijingfeiController extends BaseController
{
    private String prefix = "system/danweijingfei";

    @Autowired
    private IJingfeiDanweijingfeiService jingfeiDanweijingfeiService;

    @RequiresPermissions("system:danweijingfei:view")
    @GetMapping()
    public String danweijingfei()
    {
        return prefix + "/danweijingfei";
    }

    /**
     * 查询deptjingfei列表
     */
    @RequiresPermissions("system:danweijingfei:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JingfeiDanweijingfei jingfeiDanweijingfei)
    {
        startPage();
        List<JingfeiDanweijingfei> list = jingfeiDanweijingfeiService.selectJingfeiDanweijingfeiList(jingfeiDanweijingfei);
        return getDataTable(list);
    }

    /**
     * 导出deptjingfei列表
     */
    @RequiresPermissions("system:danweijingfei:export")
    @Log(title = "deptjingfei", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JingfeiDanweijingfei jingfeiDanweijingfei)
    {
        List<JingfeiDanweijingfei> list = jingfeiDanweijingfeiService.selectJingfeiDanweijingfeiList(jingfeiDanweijingfei);
        ExcelUtil<JingfeiDanweijingfei> util = new ExcelUtil<JingfeiDanweijingfei>(JingfeiDanweijingfei.class);
        return util.exportExcel(list, "deptjingfei数据");
    }

    /**
     * 新增deptjingfei
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存deptjingfei
     */
    @RequiresPermissions("system:danweijingfei:add")
    @Log(title = "deptjingfei", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JingfeiDanweijingfei jingfeiDanweijingfei)
    {
        return toAjax(jingfeiDanweijingfeiService.insertJingfeiDanweijingfei(jingfeiDanweijingfei));
    }

    /**
     * 修改deptjingfei
     */
    @RequiresPermissions("system:danweijingfei:edit")
    @GetMapping("/edit/{deptid}")
    public String edit(@PathVariable("deptid") String deptid, ModelMap mmap)
    {
        JingfeiDanweijingfei jingfeiDanweijingfei = jingfeiDanweijingfeiService.selectJingfeiDanweijingfeiByDeptid(deptid);
        mmap.put("jingfeiDanweijingfei", jingfeiDanweijingfei);
        return prefix + "/edit";
    }

    /**
     * 修改保存deptjingfei
     */
    @RequiresPermissions("system:danweijingfei:edit")
    @Log(title = "deptjingfei", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JingfeiDanweijingfei jingfeiDanweijingfei)
    {
        return toAjax(jingfeiDanweijingfeiService.updateJingfeiDanweijingfei(jingfeiDanweijingfei));
    }

    /**
     * 删除deptjingfei
     */
    @RequiresPermissions("system:danweijingfei:remove")
    @Log(title = "deptjingfei", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jingfeiDanweijingfeiService.deleteJingfeiDanweijingfeiByDeptids(ids));
    }

    @RequestMapping("findone")
    @ResponseBody
    public JsonResult<?> findOne(@RequestParam("deptid")String ppDeptId){
        try{
            return JsonResult.getSuccessResult(jingfeiDanweijingfeiService.selectJingfeiDanweijingfeiByDeptid(ppDeptId));

        } catch (Exception e) {
            logger.error("danweijingfei -> findbyparentid: "+e.getMessage());
            return JsonResult.getErrorResult("查询单位经费失败");
        }
    }

    @RequestMapping("findbyparentid")
    @ResponseBody
    public JsonResult<?> findByParentId(@RequestParam("parentid")String ppParentId){
        try{
            return JsonResult.getSuccessResult(jingfeiDanweijingfeiService.findByParentId(ppParentId));

        } catch (Exception e) {
            logger.error("danweijingfei -> findbyparentid: "+e.getMessage());
            return JsonResult.getErrorResult("查询单位经费失败");
        }
    }

//    @Log(title = "更新单位经费明细", businessType = BusinessType.UPDATE)
//    @PostMapping("/updateandfindbydeptid")
//    @ResponseBody
//    public JsonResult UpdateAndFindByDeptId(@RequestParam("deptid") String ppDeptid, HttpServletRequest request) {
//
//        try {
//            String[] mmdeptIds = ppDeptids.split(",");
//            String[] mmzhuanjiaIds = ppZhuanjiaids.split(",");
//            // 经费是否发放   用到的变动需要改为未变动
//            HttpSession session = request.getSession();
//            String deptid = session.getAttribute("deptid").toString(); // 单位id
//            BooleanMessage mm发放 = jisuanService.Deptfafang(ppPiciid,deptid,mmdeptIds,mmzhuanjiaIds);
//            if(mm发放.isOk()){
//                return JsonResult.getSuccessResult("发放成功");
//            }else{
//                return  JsonResult.getErrorResult(mm发放.getMessage().toString());
//            }
//        } catch (Exception e) {
//            logger.error("jisuan -> deptfafang: "+e.getMessage());
//            return JsonResult.getErrorResult("单位经费发放失败");
//        }
//    }
}
